package com.Iconcat.system.util;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import javax.servlet.http.HttpServlet;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Random;

public class VertiCodeUtill extends HttpServlet {
    private  String str="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private ByteArrayInputStream image;
    private int width=120;
    private int height=50;
    private StringBuilder vercode;

    private VertiCodeUtill() {
        init();
    }

    public StringBuilder getVercode() {
        return vercode;
    }

    public void setVercode(StringBuilder vercode) {
        this.vercode = vercode;
    }

    public static VertiCodeUtill getInstance(){
        return new VertiCodeUtill();
    }

    public ByteArrayInputStream getImage() {
        return image;
    }

    public void setImage(ByteArrayInputStream image) {
        this.image = image;
    }
    public String getStr() {
        return str;
    }

    public void setStr(String str) {
        this.str = str;
    }
    public void init(){
        final BufferedImage image=new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB);
      final Graphics2D g=(Graphics2D)image.getGraphics();
      Random random=new Random();
      g.setColor(getRandColor(200,250));
      g.fillRect(0,0,width,height);
//      for (int i=0;i<4;i++){
//       final String rand= String.valueOf(random.nextInt(10));
//       g.setColor(getRandColor(28,110));
//       g.drawString(rand,(width/4)*i,46);
//      }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i <4; i++) {
            g.setFont(new Font("Times New Roman",Font.PLAIN| Font.HANGING_BASELINE,35));
            g.setColor(getRandColor(28,110));
            int rand=random.nextInt(str.length());
            String ch=str.charAt(rand)+"";
            sb.append(ch);
            g.drawString(ch,(width/4)*i,40);
        }
       vercode=sb;
        // 绘制干扰线
        g.setColor(Color.DARK_GRAY);
        g.setStroke(new BasicStroke(1.5F,BasicStroke.CAP_ROUND,BasicStroke.JOIN_MITER));
        for (int i = 0; i < 10; i++) {
            int startx = random.nextInt(width);
            int starty = random.nextInt(height);
            int endx = random.nextInt(width);
            int endy = random.nextInt(height);
            g.drawLine(startx, starty, endx, endy);
        }
        g.dispose();
//        System.out.println(g.getFont());
        ByteArrayInputStream inputStream=null;
        ByteArrayOutputStream outputStream=new ByteArrayOutputStream();


        try{
            final ImageOutputStream imageOut=ImageIO.createImageOutputStream(outputStream);
            ImageIO.write(image,"JPEG",imageOut);
            imageOut.close();
            inputStream=new ByteArrayInputStream(outputStream.toByteArray());
        }catch (IOException e) {
            System.out.println("验证码生成失败"+e);
        }
        this.image=inputStream;

    }
    private Color getRandColor(int fc,int bc){
        Random random=new Random();
        if(fc>255){fc=255;}
        if(bc>255){bc=255;}
        int r=fc+random.nextInt(bc-fc);
        int g=fc+random.nextInt(bc-fc);
        int b=fc+random.nextInt(bc-fc);
        return  new Color(r,g,b);
    }

}
